package com.example.ccj.b.c.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.example.ccj.R;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ScreenUtils;
import com.example.ccj.u.SizeUtils;

import java.util.ArrayList;

/**
 * 放置功能按钮的控件
 */
public class HeadUI extends LinearLayout {

    RecyclerView recyclerView;
    TextView mText;
    ArrayList<HeadUIAdapterUtil> headUIAdapterUtils;
    LinearLayout title;
    ImageView rightImg;
    HeadUIAdapter headUIAdapter;

    public HeadUI(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.ui_head_ui,this);
        recyclerView = findViewById(R.id.ui_head_rec);
        mText = findViewById(R.id.ui_head_text);
        title = findViewById(R.id.ui_head_title);
        rightImg = findViewById(R.id.ui_head_right_img);
        headUIAdapterUtils = new ArrayList<>();

        //获取设置的名称
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.HeadUI);

        String headUiName = typedArray.getString(R.styleable.HeadUI_name);
        if (headUiName != null) mText.setText(headUiName);
        typedArray.recycle();

        int appScreenWidth = ScreenUtils.getAppScreenWidth();
        int dpWidth = SizeUtils.px2dp(appScreenWidth);
        int i = dpWidth / 90;
//        LogUtils.i("横向加载图片控件-屏幕宽度px:" + appScreenWidth+",屏幕宽度dp:" + dpWidth+",加载数量:" + i);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(i, StaggeredGridLayoutManager.VERTICAL);
        // 设置RecyclerView的LayoutManager
        headUIAdapter = new HeadUIAdapter(headUIAdapterUtils);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(headUIAdapter);
    }

    public static class HeadUIAdapterUtil {
        public String text;
        public int image;
        public boolean isNetwork = false;
        public String urlPath;
        public OnClickListener itemClickListener;
        public int jiaobiao;
        public int itemId;
    }

    //设置模块标题
    public void setHeadText(String head){
        this.title.setVisibility(VISIBLE);
        this.mText.setText(head);
    }

    public void setRightImg(int imgId,@NonNull View.OnClickListener clickListener){
        this.title.setVisibility(View.VISIBLE);
        this.rightImg.setVisibility(View.VISIBLE);
        this.rightImg.setImageResource(imgId);
        this.rightImg.setOnClickListener(clickListener);
    }

    /**
     *
     * @param icon 图片
     * @param text 文本
     * @param clickListener 点击事件
     */
    public void setIconInformation(int icon, String text, OnClickListener clickListener){
        HeadUIAdapterUtil util = new HeadUIAdapterUtil();
        util.image = icon;
        util.text = text;
        util.itemClickListener = clickListener;
        setIconInformation(util);
    }

    public void setIconInformation(HeadUIAdapterUtil headUIAdpaterUtil){
        headUIAdapterUtils.add(headUIAdpaterUtil);
        if (recyclerView.getAdapter() != null) headUIAdapter.notifyItemInserted(headUIAdapterUtils.size()-1);
    }

    public void setJiaoBiao(int itemId,int sum){
        for (HeadUIAdapterUtil headUIAdpaterUtil : headUIAdapterUtils) {
            if (headUIAdpaterUtil.itemId == itemId) {
                headUIAdpaterUtil.jiaobiao = sum;
                if (recyclerView.getAdapter() != null) {
                    recyclerView.getAdapter().notifyDataSetChanged();
                }
                break;
            }
        }
    }

    /**
     * 加载网络图片
     * @param text 文本
     * @param icon  图片
     * @param clickListener 点击事件
     */
    public void setIconInformation(String text,String icon,  OnClickListener clickListener){
        HeadUIAdapterUtil util = new HeadUIAdapterUtil();
        util.urlPath = icon;
        util.isNetwork = true;
        util.text = text;
        util.itemClickListener = clickListener;
        headUIAdapterUtils.add(util);
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public TextView getmText() {
        return mText;
    }

    public ArrayList<HeadUIAdapterUtil> getHeadUIAdapterUtils() {
        return headUIAdapterUtils;
    }

    public LinearLayout getTitle() {
        return title;
    }

    public ImageView getRightImg() {
        return rightImg;
    }

    public HeadUIAdapter getHeadUIAdapter() {
        return headUIAdapter;
    }
}
